package com.zzyy.study.aop;

import com.zzyy.study.resp.ResultData;
import com.zzyy.study.trace.TraceUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * @auther zzyy
 * @create 2024-04-18 19:43
 */
@Order
@Aspect
@Component
public class ResultTraceIdAspect
{
    @Pointcut("execution(* com.zzyy..*Controller.*(..)) || execution(* com.zzyy.study.exp.GlobalExceptionHandler.*(..))")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        Object result = null;

        result = proceedingJoinPoint.proceed(); //放行

        if (result instanceof ResultData)
        {
            ((ResultData<?>) result).setTraceId(TraceUtils.getTraceId());
        }

        return result;
    }
}
